package com.bytedance.service.impl;

import com.bytedance.dao.ADAO;
import com.bytedance.dao.BDAO;
import com.bytedance.dao.CDAO;
import com.bytedance.dataobject.C;
import com.bytedance.model.Result;
import com.bytedance.service.CService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CServiceImpl implements CService {

    @Autowired
    private CDAO cDAO;

    @Autowired
    private BDAO bDAO;

    @Autowired
    private ADAO aDAO;

    @Override
    public Result<C> add(C c) {
        Result<C> result = new Result<>();
        C getC = cDAO.getCByB(c.getB());
        if (getC != null) {
            result.setResultFailed("该对象C已存在！");
            return result;
        }
        setCField(c);
        cDAO.addC(c);
        result.setResultSuccess("添加对象C成功！", c);
        return result;
    }

    @Override
    public Result<C> update(C c) throws Exception {
        Result<C> result = new Result<>();
        C getC = cDAO.getCByB(c.getB());
        if (getC == null) {
            result.setResultFailed("该对象C不存在！");
            return result;
        }
        setCField(c);
        cDAO.updateC(c);
        result.setResultSuccess("更新对象C成功！", c);
        return result;
    }


    @Override
    public Result<List<C>> getAll() {
        Result<List<C>> result = new Result<>();
        result.setResultSuccess("获取所有对象C成功！", cDAO.getCList());
        return result;
    }

    @Override
    public Result<List<C>> getByB(String b) {
        Result<List<C>> result = new Result<>();
        result.setResultSuccess("获取对象C成功！", cDAO.getCListByB(b));
        return result;
    }

    private void setCField(C c) {
        float AAaCount = aDAO.getCount("aa");
        float ABbCount = aDAO.getCount("bb");
        float ACcCount = aDAO.getCount("cc");
        float ADdCount = aDAO.getCount("dd");
        float AEeCount = aDAO.getCount("ee");
        float BAaCount = bDAO.getCount("aa");
        float BBbCount = bDAO.getCount("bb");
        float BCcCount = bDAO.getCount("cc");
        float aaCorrectCount = bDAO.getCount("aa_correct");
        float bbCorrectCount = bDAO.getCount("bb_correct");
        float ccCorrectCount = bDAO.getCount("cc_correct");
        float ddCorrectCount = bDAO.getCount("dd_correct");
        float eeCorrectCount = bDAO.getCount("ee_correct");

        if (AAaCount != 0) {
            c.setAaS(aaCorrectCount / AAaCount);
        }
        if (ABbCount != 0) {
            c.setBbS(bbCorrectCount / ABbCount);
        }
        if (ACcCount != 0) {
            c.setCcS(ccCorrectCount / ACcCount);
        }
        if (ADdCount != 0) {
            c.setDdS(ddCorrectCount / ADdCount);
        }
        if (BAaCount != 0) {
            c.setAaA(aaCorrectCount / BAaCount);
        }
        if (BBbCount != 0) {
            c.setBbA(bbCorrectCount / BBbCount);
        }
        if (BCcCount != 0) {
            c.setCcA(ccCorrectCount / BCcCount);
        }
        if (AEeCount != 0) {
            c.setAaSS(eeCorrectCount / AEeCount);
        }
        if ((AAaCount + ABbCount) != 0) {
            c.setAaC((aaCorrectCount + bbCorrectCount) / (AAaCount + ABbCount));
        }
    }
}
